﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Data;

namespace WebUI.BL.Products
{
    public class ProductColorBL
    {
        public void Insert(tbl_ProductColor productColor)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductColor newproductColor = new tbl_ProductColor()
                    {
                        Status=productColor.Status,
                        Img=productColor.Img,
                        CreateUser=productColor.CreateUser,
                        CreatedDate=productColor.CreatedDate,
                        ColorName=productColor.ColorName,
                        ColorDesc=productColor.ColorDesc,
                        ColorCode=productColor.ColorCode
                    };
                    contextdb.tbl_ProductColors.InsertOnSubmit(newproductColor);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductColor productColor)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateproductColor = contextdb.tbl_ProductColors.SingleOrDefault(u => u.ColorNo == productColor.ColorNo);
                try
                {
                    if (updateproductColor != null)
                    {
                        updateproductColor.Status=productColor.Status;
                        updateproductColor.Img=productColor.Img;
                        updateproductColor.CreateUser=productColor.CreateUser;
                        updateproductColor.CreatedDate=productColor.CreatedDate;
                        updateproductColor.ColorName=productColor.ColorName;
                        updateproductColor.ColorDesc=productColor.ColorDesc;
                        updateproductColor.ColorCode = productColor.ColorCode;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int productColorNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductColor = contextdb.tbl_ProductColors.SingleOrDefault(u => u.ColorNo == productColorNo);
                try
                {
                    if (deleteproductColor != null)
                    {
                        contextdb.tbl_ProductColors.DeleteOnSubmit(deleteproductColor);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void DeleteByStatus(int productColorNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductColor = contextdb.tbl_ProductColors.SingleOrDefault(u => u.ColorNo == productColorNo);
                try
                {
                    if (deleteproductColor != null)
                    {
                        deleteproductColor.Status = "Deleted";
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }


        //public List<ModelSpAdminSearchProductColor> GetProductColors(string colorCode, string name, string fromdate, string toDate,string status)
        //{
        //    var contextdb = new AdlerContextDataContext();
        //    IQueryable<tbl_ProductColor> queryable = contextdb.tbl_ProductColors;
        //    if (!String.IsNullOrEmpty(colorCode))
        //        queryable = contextdb.tbl_ProductColors.Where(x => x.ColorCode == colorCode);
        //    if (!String.IsNullOrEmpty(name))
        //        queryable = queryable.Where(x => x.ColorName.Contains(name));
        //    if (!String.IsNullOrEmpty(fromdate))
        //        queryable = queryable.Where(x => x.CreatedDate >= Convert.ToDateTime(fromdate));
        //    if (!String.IsNullOrEmpty(toDate))
        //        queryable = queryable.Where(x => x.CreatedDate <= Convert.ToDateTime(toDate));

        //    var p = queryable.AsEnumerable()
        //        .Select((n, i) => new ModelSpAdminSearchProductColor
        //                              {
        //                                  STT = i + 1,
        //                                  ColorCode = n.ColorCode,
        //                                  ColorName = n.ColorName,
        //                                  ColorDesc = n.ColorDesc,
        //                                  ColorNo = n.ColorNo.ToString(),
        //                                  CreateDate = n.CreatedDate.Value.ToShortDateString(),
        //                                  Img = n.Img,
        //                                  CreateUser = n.CreateUser,
        //                                  Status = n.Status
        //                              }).OrderByDescending(x => x.ColorNo);

        //    //var x = from n in contextdbSP_ADMIN_SEARCH_ProductColor(name, colorCode, status, fromdate, toDate)
        //    //       select new MODEL_SP_ADMIN_SEARCH_ProductColor
        //    //        {
        //    //            STT = index++,
        //    //            ColorCode = n.ColorCode,
        //    //            ColorName=n.ColorName,
        //    //            ColorDesc = n.ColorDesc,
        //    //            ColorNo = n.ColorNo.ToString(),
        //    //            CreateDate = n.CreatedDate.Value.ToShortDateString(),
        //    //            Img = n.Img,
        //    //            CreateUser=n.CreateUser,
        //    //            Status = n.Status
        //    //        };
        //    //return x.ToList<MODEL_SP_ADMIN_SEARCH_ProductColor>();
        //    return p.ToList();

        //}

        public tbl_ProductColor GetProductColorById(int id)
        {
            var dbcontext = new AdlerContextDataContext();
            return dbcontext.tbl_ProductColors.FirstOrDefault(x => x.ColorNo == id);
        }

        /// <summary>
        /// Mau, cach 2.
        /// </summary>
        /// <param name="colorCode"></param>
        /// <param name="name"></param>
        /// <param name="fromdate"></param>
        /// <param name="toDate"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        //public IEnumerable<T> GetProductColors1<T>(Func<object,object,object,object,object,object,object,object,T> creator ,
        //    string colorCode, string name, string fromdate, string toDate, string status,int currentPage,int pageSize) where T:class
        //{
        //    var contextdb = new AdlerContextDataContext();
        //    var queryable = contextdb.tbl_ProductColors.AsQueryable<tbl_ProductColor>();

            
        //    if (!String.IsNullOrEmpty(colorCode))
        //        queryable = queryable.Where(x => x.ColorCode == colorCode);
        //    if (!String.IsNullOrEmpty(name))
        //        queryable = queryable.Where(x => x.ColorName.Contains(name));
        //    if (!String.IsNullOrEmpty(fromdate))
        //        queryable = queryable.Where(x => x.CreatedDate > Convert.ToDateTime(fromdate));
        //    if (!String.IsNullOrEmpty(toDate))
        //        queryable = queryable.Where(x => x.CreatedDate <= Convert.ToDateTime(toDate));
        //    string s = queryable.ToString();
        //    //lay gia tri de phan trang
        //    if(currentPage!=0)
        //    {
        //        var newquery = queryable.AsEnumerable().
        //            OrderByDescending(u => u.ColorNo).
        //            Skip((currentPage - 1)*pageSize)
        //            .Take(pageSize).Select(n => creator(
        //                n.ColorCode,
        //                n.ColorName,
        //                n.ColorDesc,
        //                n.ColorNo,
        //                n.CreatedDate,
        //                n.Img,
        //                n.CreateUser,
        //                n.Status));
        //        return newquery;
        //    }
        //    //dang cho tim kiem, tim kiem het cac gia tri o duoi db
        //    var p = queryable.AsEnumerable()
        //        .Select(n => creator(
        //            n.ColorCode,
        //            n.ColorName,
        //            n.ColorDesc,
        //            n.ColorNo,
        //            n.CreatedDate,
        //            n.Img,
        //            n.CreateUser,
        //            n.Status
                    
        //                         ));
     
        //    return p;

        //}

        public int CountProductColor()
        {
            var dbcontext = new AdlerContextDataContext();
            return dbcontext.tbl_ProductColors.Count();
        }

        // Show Product By Product.
        public List<MODEL_SP_FONT_PRODUCT_COLOR> Get_Font_ProductColor(int productno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var query = from n in contextdb.SP_FONT_PRODUCT_COLOR(productno)
                            select new MODEL_SP_FONT_PRODUCT_COLOR
                            {
                                ColorNo = n.ColorNo,
                                ColorName = n.ColorName,
                                Img = n.Img
                            };
                return query.ToList();
            }
        }

        // Show All Color.
        public DataTable Get_ColorAll()
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                DataTable dt = new DataTable();
                var query = contextdb.SP_FONT_COLOR();
                dt = query.ConvertToDataTable(record => new object[] { query });
                return dt;
            }
        }
    }
}
